package shiyan4;

import java.io.IOException;
import java.net.*;

public class Client {
    public static void main(String[] args) {
        String group = "239.1.2.3";
        int port = 8080;
        try (MulticastSocket socket = new MulticastSocket(8080)) {
            socket.setReuseAddress(true);
            InetAddress groupAddress = InetAddress.getByName(group);
            socket.joinGroup(groupAddress);
            System.out.println("加入组广播");
            byte[] buf = new byte[1024];
            while (true) {
                DatagramPacket packet = new DatagramPacket(buf, buf.length);
                socket.receive(packet);
                String resp = new String(packet.getData(), 0, packet.getLength());
                System.out.println("收到来自"+packet.getAddress()+":"+packet.getPort()+"的消息"+resp);

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
